﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XemlUtilities.ISA.Ontology;
using XemlUtilities.DataSet;

namespace XemlUtilities.ISA.Protocol
{
    class Growth : ProtocolBase
    {
        public XemlDataSet xds { get; private set; }

        #region constructors
        public Growth(XemlDataSet xds)
            : base()
        {
            Name = "growth protocol";
            Type = "growth";
            Description = "The growth conditions are described using XEML (example <a href=\"http://gmd.mpimp-golm.mpg.de/profile/\">http://gmd.mpimp-golm.mpg.de/profile/</a>). The XEML framework (XEML interactive designer and xml schema) has been created to assist plant biologists in designing and documenting simple or sophisticated experiments in a machine readable format, to link this metadata with any type of data generated in the corresponding experiments, and ultimately, to make both metadata and data available for data mining.";
            this.xds = xds;
            this.Register();
            foreach (var item in GetGrowthSteps()) { } // this makes sure all terms and ontologies used in this protocol get registered
        }
        #endregion constructors

        public override IEnumerable<Parameter> GetParameters()
        {
            foreach (String growthStep in xds.Value.Select(x => new GrowthStep(x)).Distinct().OrderBy(x => x.time).ThenBy(x => x.term.Name).ThenBy(x => x.Context).Select(x => x.ToString()))
            {
                Parameter p = new Parameter();
                p.Name = growthStep;

                yield return p;
            }
        }

        public IEnumerable<GrowthStep> GetGrowthSteps()
        {
            // TODO Make sure that when selecting growthsteps, only to select from ontologies that represent growthsteps!
            return xds.Value.Select(x => new GrowthStep(x)).Where(x => x.time.HasValue).Distinct().OrderBy(x => x.term.Name).ThenBy(x => x.Context).ThenBy(x => x.time);
        }

        public struct GrowthStep
        {
            public short id;
            public Term term;
            public double? time;
            public string Context;
            public short cycleId;
            //public string NameSpace;

            public GrowthStep(XemlDataSet.ValueRow x)
            {
                XemlDataSet.TermRow tr = (x.Table.DataSet as XemlDataSet).Term.Single(t => t.Id == x.FK_Term);

                String ontologyAcronym = (x.Table.DataSet as XemlDataSet).Ontology.Single(o => o.Id == (x.Table.DataSet as XemlDataSet).Term.Single(t => t.Id == x.FK_Term).FK_Ontology).NameSpaceAlias;
                OntologyBase.GetOrCreateOntology(ontologyAcronym);

                this.id = x.FK_Term;
                this.term = OntologyBase.GetOrCreateTerm(ontologyAcronym, tr.TermId, tr.Name);
                if (!x.IsTimeNull())
                {
                    this.time = x.Time;
                }
                else
                {
                    this.time = null;
                }
                this.Context = x.Context;
                this.cycleId = x.CycleId;
            }

            public override string ToString()
            {
                if (time.HasValue)
                {
                    TimeSpan ts = TimeSpan.FromSeconds(time.Value);
                    return String.Format("{0}_{1}_after_{2}", term.Name, Context, ts.StyleUpXemlFormat());
                }
                return String.Format("{0}_{1}", term.Name, Context);
            }
        }
    }
}
